package com.bom.boot.easy.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.bom.boot.easy.core.Result;
import com.bom.boot.easy.facade.dto.BaseDTO;
import com.bom.boot.easy.service.BomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("core")
@AllArgsConstructor
@Log4j2
public class BomController {

    private final BomService bomService;

    @PostMapping(value = "search/{ds}/{key}")
    @Operation(summary = "查询列表(没有返回结果提示和格式化)")
    @ResponseBody
    public Result<List<Map<String, Object>>> search(@Schema(title = "数据库名称", example = "MAIN") @PathVariable("ds") String ds, @Schema(title = "查询名称", example = "search") @PathVariable("key") String key, @Schema(title = "条件 分页和变量") @RequestBody BaseDTO dto) {
        dto.put("_ds", ds);
        dto.put("_key", key);
        return bomService.search(dto);
    }

    @PostMapping(value = "detail/{ds}/{key}")
    @Operation(summary = "查询详情(没有返回结果提示和格式化)")
    @ResponseBody
    public Result<Map<String, Object>> detail(@Schema(title = "数据库名称", example = "MAIN") @PathVariable("ds") String ds, @Schema(title = "查询名称", example = "detail") @PathVariable("key") String key, @Schema(title = "条件 id和变量") @RequestBody BaseDTO dto) {
        dto.put("_ds", ds);
        dto.put("_key", key);
        return bomService.detail(dto);
    }

    @PostMapping(value = "{pageName}/{optionName}")
    @Operation(summary = "执行操作(格式化和参数验证)")
    @ResponseBody
    public Result<Object> execute(@PathVariable("pageName") String pageName, @PathVariable("optionName") String optionName, @RequestBody BaseDTO dto) {
        dto.put("_page", StrUtil.toUnderlineCase(pageName).replaceAll("_", "/"));
        dto.put("_option", optionName);
        return bomService.execute(dto);
    }
}
